import { carFullInfo } from "~/src/constant"
import { utils, writeFile } from "xlsx"

export function exportXlsx(carInfo: Car.CarInfo) {
    const excelData: string[][] = [
        ['', '', '', '', '', '', '', '（   ）年油字第   号', '', ''],
        ['', '加油指标签发单（加油单）', '', '', '', '', '', '', '', ''],
        ['', '', '', '申请时间：', '', '', '', '', '', ''],
        ['序号', '用油单位', '', '', '', '名称', '', '', '', ''],
        ['', ...carFullInfo.map(item => `${item.title}`), '油料员', '备注'],
        ['', '', '', '', '', '', '', '', '', ''],
        ['1', ...carFullInfo.map(item => `${carInfo[item.key as keyof Car.CarInfo]}`), '', ''],
        ['', '', '', '', '', '', '', '', '', ''],
        ['', '', '', '', '', '', '', '', '', ''],
        ['', '', '', '', '', '', '', '', '', ''],
        ['合计', '', '', '', '', '', '', '', '', ''],
    ]
    const options: Object[] = Array.from({ length: excelData[0].length }).map(() => ({ wpx: 100 }))
    const worksheet = utils.aoa_to_sheet(excelData)
    worksheet['!cols'] = options
    worksheet['!merges'] = [
        { e: { c: 9, r: 0 }, s: { c: 7, r: 0 } },
        { e: { c: 9, r: 1 }, s: { c: 1, r: 1 } },
        { e: { c: 9, r: 2 }, s: { c: 3, r: 2 } },
        { e: { c: 9, r: 3 }, s: { c: 6, r: 3 } },
        { e: { c: 4, r: 3 }, s: { c: 2, r: 3 } },
        { e: { c: 0, r: 3 }, s: { c: 0, r: 5 } },
        ...Array.from({ length: excelData[0].length - 1 }).map((_, index) => ({ e: { c: index + 1, r: 4 }, s: { c: index + 1, r: 5 } })),
        { e: { c: 6, r: 10 }, s: { c: 0, r: 10 } }]
    const workbook = utils.book_new()
    utils.book_append_sheet(workbook, worksheet, 'Sheet1')
    writeFile(workbook, '加油单.xlsx')
}